﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace ThreadCallback
{
    public partial class Form1 : Form
    {
        //声明委托
        private delegate void WriteTextBox(char ch);
        //定义委托
        private WriteTextBox writeTextBox;
        public Form1()
        {
            InitializeComponent();
            //允许跨线程调用
            CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread tsk1Thread = new Thread(new ThreadStart(doTask1));
            Thread tsk2Thread = new Thread(new ThreadStart(doTask2));
            tsk1Thread.Start();
            tsk2Thread.Start();
        }

        private void doTask2()
        {
            if (checkBox1.Checked == true)
            {
                groupBox1.Text = "运行中.....";
                groupBox1.Refresh();
                textBox1.Clear();
                textBox1.Refresh();
                //定义委托
                writeTextBox = new WriteTextBox(WriteTextBox1);
                //将方法作为参数传入
                WriTxt(writeTextBox);
                groupBox1.Text = "任务1";
                textBox3.Focus();
                textBox3.SelectAll();
            }
        }

        private void doTask1()
        {
            if (checkBox2.Checked == true)
            {
                groupBox2.Text = "运行中.....";
                groupBox2.Refresh();
                textBox2.Clear();
                textBox2.Refresh();
                //定义委托
                writeTextBox = new WriteTextBox(WriteTextBox2);
                //将方法作为参数传入
                WriTxt(writeTextBox);
                groupBox2.Text = "任务2";
                textBox3.Focus();
                textBox3.SelectAll();
            }
        }
        private void writeTextBox2()
        {
            string strData = textBox3.Text;
            for (int i = 0; i < strData.Length; i++)
            {
                textBox2.AppendText(strData[i] + "\r");
                DateTime now = DateTime.Now;
                while (now.AddSeconds(0.5) > DateTime.Now) { }
            }
        }

        private void writeTextBox1()
        {
            string strData = textBox3.Text;
            for (int i = 0; i < strData.Length; i++)
            {
                textBox1.AppendText(strData[i] + "\r");
                DateTime now = DateTime.Now;
                while (now.AddSeconds(0.5) > DateTime.Now) { }
            }
        }

        private void WriteTextBox1(char ch)
        {
            textBox1.AppendText(ch + "\r");
        }

        private void WriteTextBox2(char ch)
        {
            textBox2.AppendText(ch + "\r");
        } 

        private void WriTxt(WriteTextBox writeTextBox)
        {
            string strData = textBox3.Text;
            for (int i = 0; i < strData.Length; i++)
            {
                writeTextBox(strData[i]);
                DateTime now = DateTime.Now;
                while (now.AddSeconds(0.5) > DateTime.Now) { }
            }
        }
    }
}


